package com.nwpu.coursesupport.service.imple;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.Status;
import com.nwpu.coursesupport.enumeration.TaskType;
import com.nwpu.coursesupport.pagination.PageBounds;
import com.nwpu.coursesupport.view.TaskGradeListView;
import com.nwpu.coursesupport.view.criteria.TaskGradeListViewSearchCriteria;
import com.nwpu.coursesupport.view.mapper.TaskGradeListViewMapper;
@Service
public class TaskGradeListViewService {
	
	@Autowired
	private TaskGradeListViewMapper  taskGradeListViewMapper;
	
    protected static Logger logger = LoggerFactory.getLogger(TaskGradeListViewService.class);

	
	public JSONResult listView(TaskGradeListViewSearchCriteria sc){
		JSONResult rst=new JSONResult();
		rst.fail();
		
		if(sc==null||sc.getCourseId()==null||sc.getUid()==null){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
		
		if(sc.getType()==null){
			List<TaskGradeListView> results=new ArrayList<TaskGradeListView>();
			results.addAll(taskMainListView(sc,Status.NO,rst));
			Collections.sort(results);
			int pageSize = 40;
	        int totalCount =results.size();
	        int pageCount = totalCount % pageSize > 0 ? totalCount / pageSize + 1 : totalCount / pageSize;
			if (sc.getPageNo() > pageCount){
				sc.setPageNo(pageCount);
			}
			if (sc.getPageNo() <= 0){
				sc.setPageNo(1);
			}
			int index=pageSize*(sc.getPageNo()-1);
			int toIndex=(index+20)<results.size()?(index+20):results.size();
			results=results.subList(index, toIndex);
			rst.addData("list", results);
			rst.addData("pageNo", sc.getPageNo());
		    rst.addData("allpages", pageCount);
		}else if(sc.getType()==TaskType.HOMEWORK){
			taskMainListView(sc,Status.YES,rst);
		}else if(sc.getType()==TaskType.LABTEST){
			taskMainListView(sc,Status.YES,rst);
		}else if(sc.getType()==TaskType.TESTINLESSON){
			taskMainListView(sc,Status.YES,rst);
		}
		
		rst.success();
		return rst;
	}
	
	public List<TaskGradeListView> taskMainListView(TaskGradeListViewSearchCriteria sc,Status isPage,JSONResult rst){
		if(isPage==Status.NO){
			List<TaskGradeListView> list = this.taskGradeListViewMapper.findTaskMainList(sc);
			return list;
		}else{
			if (sc.getPageNo() <= 0){
				sc.setPageNo(1);
			}
	        
	        int pageSize = 40;
	        int totalCount =this.taskGradeListViewMapper.countTaskMainList(sc);
	        int pageCount = totalCount % pageSize > 0 ? totalCount / pageSize + 1 : totalCount / pageSize;
			if (sc.getPageNo() > pageCount){
				sc.setPageNo(pageCount);
			}
			PageBounds page = new PageBounds();
	        page.setNumber(sc.getPageNo());
	        page.setSize(pageSize);
	        List<TaskGradeListView> list = this.taskGradeListViewMapper.findTaskMainList(sc,page);
	        rst.addData("list", list);
	        rst.addData("pageNo", sc.getPageNo());
	        rst.addData("allpages", pageCount);
	        return null;
		}
	}

	public List<TaskGradeListView> taskPersonalListView(TaskGradeListViewSearchCriteria sc,Status isPage,JSONResult rst){
		sc.setType(TaskType.PERSONALTASK);
		if(isPage==Status.NO){
			List<TaskGradeListView> list = this.taskGradeListViewMapper.findTaskPersonalList(sc);
			return list;
		}else{
			if (sc.getPageNo() <= 0){
				sc.setPageNo(1);
			}
	        int pageSize = 40;
	        int totalCount =this.taskGradeListViewMapper.countTaskPersonalList(sc);
	        int pageCount = totalCount % pageSize > 0 ? totalCount / pageSize + 1 : totalCount / pageSize;
			if (sc.getPageNo() > pageCount){
				sc.setPageNo(pageCount);
			}
			PageBounds page = new PageBounds();
	        page.setNumber(sc.getPageNo());
	        page.setSize(pageSize);
	        List<TaskGradeListView> list = this.taskGradeListViewMapper.findTaskPersonalList(sc,page);
	        rst.addData("list", list);
	        rst.addData("pageNo", sc.getPageNo());
	        rst.addData("allpages", pageCount);
	        return null;
		}
	}
	
	public List<TaskGradeListView> taskInLessonListView(TaskGradeListViewSearchCriteria sc,Status isPage,JSONResult rst){
		sc.setType(TaskType.ASKINLESSON);
		
		if(isPage==Status.NO){
			List<TaskGradeListView> list = this.taskGradeListViewMapper.findTaskInLessonList(sc);
			return list;
		}else{
			if (sc.getPageNo() <= 0){
				sc.setPageNo(1);
			}
	        int pageSize = 40;
	        int totalCount =this.taskGradeListViewMapper.countTaskInLessonList(sc);
	        int pageCount = totalCount % pageSize > 0 ? totalCount / pageSize + 1 : totalCount / pageSize;
			if (sc.getPageNo() > pageCount){
				sc.setPageNo(pageCount);
			}
			PageBounds page = new PageBounds();
	        page.setNumber(sc.getPageNo());
	        page.setSize(pageSize);
	        List<TaskGradeListView> list = this.taskGradeListViewMapper.findTaskInLessonList(sc,page);
	        rst.addData("list", list);
	        rst.addData("pageNo", sc.getPageNo());
	        rst.addData("allpages", pageCount);
	        return null;
		}
	}
}
